using System;
using System.Windows.Forms;
using RedGate.SQLDataGenerator.Engine.Generators;
using RedGate.SQLDataGenerator.Engine.Project;

namespace CodeGenerator
{
    public partial class CodeGeneratorUI : UserControl, IGeneratorSimpleUI
    {
        private readonly CodeGenerator m_CodeGenerator;
        private readonly GeneratorParameters m_GeneratorParameters;
        private ModifiedGeneratorDataEventHandler m_Modified;
        private SDGTable m_Table;
        private SDGField m_Field;

        public CodeGeneratorUI(CodeGenerator codeGenerator, 
                               GeneratorParameters generatorParameters)
        {
            m_CodeGenerator = codeGenerator;
            m_GeneratorParameters = generatorParameters;

            InitializeComponent();
        }

        public void ConfigureFieldAndTable(SDGTable table, SDGField field)
        {
            m_Table = table;
            m_Field = field;
            m_SourceTextBox.Text = m_CodeGenerator.Source;
        }

        public ModifiedGeneratorDataEventHandler Modified
        {
            get { return m_Modified; }
            set { m_Modified = value; }
        }

        private void m_SourceTextBox_TextChanged(object sender, EventArgs e)
        {
            m_CodeGenerator.Source = m_SourceTextBox.Text;

            if (m_Modified != null)
            {                
                m_Modified(this, new ModifiedGeneratorDataEventArgs(m_Table, m_Field, false));
            }           
        }

        private void m_Compile_Click(object sender, EventArgs e)
        {
            m_CodeGenerator.Compile();
            
            if (m_Modified != null)
            {
                m_Modified(this, new ModifiedGeneratorDataEventArgs(m_Table, m_Field, false));
            }
        }       
    }
}